package com.oa.dao.impl;

import java.sql.CallableStatement;
import java.sql.PseudoColumnUsage;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import com.oa.bean.SysFun;
import com.oa.dao.SysFunDao;

public class SysFunDaoImpl 
	extends CommonDao<SysFun> 
	implements SysFunDao{

	public List<SysFun> findMenuById(String id) {
		String sql="{call menu(?)}";
		List<SysFun> menus=new ArrayList<SysFun>();
		try {
			open_db();
			CallableStatement cs=conn.prepareCall(sql);
			cs.setString(1, id);
			rs=cs.executeQuery();
			while (rs.next()) {
				SysFun s=new SysFun();
				s.setDisplayName(rs.getString("displayName"));
				s.setNodeId(rs.getInt("nodeId"));
				menus.add(s);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close_db();
		}
		return menus;
	}

	public List<SysFun> findSubMenuById(String pId,String rId) {
		String sql="{call subMenu(?,?)}";
		List<SysFun> menus=new ArrayList<SysFun>();
		try {
			open_db();
			CallableStatement cs=conn.prepareCall(sql);
			cs.setString(1, pId);
			cs.setString(2, rId);			
			rs=cs.executeQuery();
			while (rs.next()) {
				SysFun s=new SysFun();
				s.setDisplayName(rs.getString("displayName"));
				s.setNodeId(rs.getInt("nodeId"));
				s.setNodeUrl(rs.getString("nodeUrl"));
				menus.add(s);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close_db();
		}
		return menus;
	}

	public List<SysFun> getMenuOrders() {
		String sql="select * from Sysfun where parentNodeId=0 order by displayOrder";
		List<SysFun> ss=new ArrayList<SysFun>();
		try {
			open_db();
			rs=conn.prepareStatement(sql).executeQuery();
			while (rs.next()) {
				SysFun sf=new SysFun();
				sf.setNodeId(rs.getInt("nodeId"));
				sf.setDisplayName(rs.getString("displayName"));
				sf.setDisplayOrder(rs.getInt("displayOrder"));
				ss.add(sf);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close_db();
		}
		return ss;
	}

	public List<SysFun> getSubMenuOrders(int parentNode){
		String sql="select * from Sysfun where parentNodeId=? order by displayOrder";
		List<SysFun> ss=new ArrayList<SysFun>();
		try {
			open_db();
			pst=conn.prepareStatement(sql);
			pst.setInt(1, parentNode);
			rs=pst.executeQuery();
			while (rs.next()) {
				SysFun sf=new SysFun();
				sf.setNodeId(rs.getInt("nodeId"));
				sf.setDisplayName(rs.getString("displayName"));
				sf.setDisplayOrder(rs.getInt("displayOrder"));
				ss.add(sf);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}finally {
			close_db();
		}
		return ss;
	}
}
